package com.example.map_20210624;

import java.util.*;

public class MapTest {

    public static void main(String[] args) {
        Map<String,Object> linkMap = new LinkedHashMap<>();
        linkMap.put("张三",20);
        linkMap.put("李四",30);
        linkMap.put("王五",40);
        linkMap.put("b",20);
        linkMap.put("c",30);
        linkMap.put("a",40);
        System.out.println(linkMap);
        System.out.println("---------------------");
        Set<Map.Entry<String, Object>> entries = linkMap.entrySet();
        entries.forEach(p->{
//            linkMap.remove("张三");
            System.out.println(p.getKey()+" = "+p.getValue());
        });
        linkMap.remove("张三");
        System.out.println(linkMap);
//        linkMap.clear();
        linkMap.replace("王五",40,41);
        System.out.println(linkMap);

        System.out.println("-----------------------");

        Map<String,Object> treeMap = new TreeMap<>();
        treeMap.put("b",20);
        treeMap.put("c",30);
        treeMap.put("a",40);
        treeMap.put("a",20);
        treeMap.put("d",30);
        treeMap.put("e",40);
        System.out.println(treeMap);

        System.out.println("--------------统计字符每个字符出现的次数-----------------");
        // 统计字符每个字符出现的次数
        String var = "aabbacddbb";
        char[] chars = var.toCharArray();
        HashMap<Character,Integer> hashMap = new HashMap<>();
//        for (char c : chars) {
//            hashMap.merge(c, 1, Integer::sum);
//        }
        for (char c : chars) {
            if(!hashMap.containsKey(c)){
                hashMap.put(c,1);
            }else{
                hashMap.put(c,hashMap.get(c)+1);
            }
        }
        System.out.println(hashMap);
    }
}
